package com.topcent.tioxyc.consumer;

import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import com.topcent.tioxyc.constants.KeyConstant;
import com.topcent.tioxyc.pojo.Assignee;
import com.topcent.tioxyc.pojo.ExpenseReimbursement;
import com.topcent.tioxyc.pojo.JdyPurchaseOrder;
import com.topcent.tioxyc.pojo.SalesOrder;
import com.topcent.tioxyc.util.ApiClient;
import com.topcent.tioxyc.util.ConnmeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: weidian
 * @date: 2024/11/11 14:21
 * @Description: 消费类
 */
@Component
@RabbitListener(queues = "feiyong")
@Slf4j
public class ExpenseReimbursementConsumer {
    private static final String DATA_CREATE = "data_create";
    private static final String DATA_UPDATE = "data_update";
    private static final String DATA_DELETE = "data_remove";

    @RabbitHandler
    public void handleMessage(String msg, Channel channel, Message message) throws IOException {
        try {
            // 在这里执行具体的业务逻辑

            // 在这里处理推送来的数据
            JSONObject payloadJSON = JSONUtil.parseObj(msg);
            String op = payloadJSON.getStr("op");
            String data = payloadJSON.getStr("data");

            if (DATA_UPDATE.equals(op)){
                update(data);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            System.out.println("Message acknowledged");
        } catch (Exception e) {
            // 处理异常并拒绝消息
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            System.err.println("发送消息失败，重试中 " + e.getMessage());
        }
    }

    private void update(String data) {
        ExpenseReimbursement bean = JSONUtil.toBean(data, ExpenseReimbursement.class);
        if (bean.getFlowState() == 1) {
            Header[] headers = ConnmeUtils.createHeaders();
            String s = ConnmeUtils.liuChen(bean.get_id());
            ExpenseReimbursement dddd =new ExpenseReimbursement();
            dddd.set_widget_1731293807186(s);
            Map<String, Object> parameterDelete = new HashMap<>();
            parameterDelete.put("instance_id", bean.get_id());
            parameterDelete.put("tasks_type", 1);
            String jsonSale = JSONUtil.toJsonStr(parameterDelete);
            JSONObject jdySale = ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/workflow/instance/get", headers, jsonSale);
            JdyPurchaseOrder jdyPurchaseOrder = JSONUtil.toBean(jdySale, JdyPurchaseOrder.class);
            int size = jdyPurchaseOrder.getTasks().size() - 1;
            boolean d=true;
            boolean c=true;
            for (int i = size; i > 0; i--) {
                if (   jdyPurchaseOrder.getTasks().get(i).getFlow_name().equals("费用会计")&&
                        jdyPurchaseOrder.getTasks().get(i).getFinish_action().equals("forward")&& d){
                    dddd.set_widget_1734080754443(jdyPurchaseOrder.getTasks().get(i).getFinish_time());
                    d=false;
                }
                // jdyPurchaseOrder.getTasks().get(i+1).getFinish_action().equals("forward") &&
                if (jdyPurchaseOrder.getTasks().get(i).getFlow_name().equals("部门负责人审核") && c){
                    dddd.set_widget_1734080754444(jdyPurchaseOrder.getTasks().get(i+1).getFinish_time());
                    c=false;
                }
            }
            dddd.set_id(bean.get_id());
            Map<String, Object> parameterInster = new HashMap<>();
            parameterInster.put("app_id", "665fbebb30a77df3f2d3d47d");
            parameterInster.put("entry_id", "665fbee388d8faa4b491f649");
            parameterInster.put("is_start_trigger", "true");
            parameterInster.put("data_id", dddd.get_id());
            dddd.set_id(null);
            parameterInster.put("data", ConnmeUtils.convertEntityToMapNull(dddd));
            String jsonSalee = JSONUtil.toJsonStr(parameterInster);
            log.info("费用单更新的数据{}", jsonSalee);
            ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSalee);
        }
    }
}
